using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Drawing;
using System.Drawing.Drawing2D;

/// <summary>
/// Summary description for ImageHelper
/// </summary>
public class ImageHelper
{
    private static int regularSizeX;

    public static int RegularSizeX
    {
        get {
            if (ConfigurationManager.AppSettings["UserPictureMaxWidth"] != null)
            {
                regularSizeX = Convert.ToInt32(ConfigurationManager.AppSettings["UserPictureMaxWidth"]);
            }
            else
            {
                regularSizeX = 100;
            }
            return regularSizeX;
        }
    }
    private static int regularSizeY;

    public static int RegularSizeY
    {
        get
        {
            if (ConfigurationManager.AppSettings["UserPictureMaxHeight"] != null)
            {
                regularSizeY = Convert.ToInt32(ConfigurationManager.AppSettings["UserPictureMaxHeight"]);
            }
            else
            {
                regularSizeY = 100;
            }
            return regularSizeY;
        }
    }
    private static int thumbSizeX;

    public static int ThumbSizeX
    {
        get
        {
            if (ConfigurationManager.AppSettings["UserPictureMaxThumbWidth"] != null)
            {
                thumbSizeX = Convert.ToInt32(ConfigurationManager.AppSettings["UserPictureMaxThumbWidth"]);
            }
            else
            {
                thumbSizeX = 25;
            }
            return thumbSizeX;
        }
       
    }
    private static int thumbSizeY;

    public static int ThumbSizeY
    {
        get
        {
            if (ConfigurationManager.AppSettings["UserPictureMaxThumbHeight"] != null)
            {
                thumbSizeY = Convert.ToInt32(ConfigurationManager.AppSettings["UserPictureMaxThumbHeight"]);
            }
            else
            {
                thumbSizeY = 100;
            }
            return thumbSizeY;
        }
       
    }

	public ImageHelper()
	{
		
	}

    public static System.Drawing.Image Resize(System.Drawing.Image source, int x1, int y1, int x2, int y2)
    {
        double x_calculated;
        double y_calculated;
        double x_offset;
        double y_offset;

        double x22 = x2;
        double y22 = y2;

        double newWidth = 0;
        double newHeight = 0;
        
        if ((source.Width > x2)||(source.Height > y2) )
        {
            if (source.Width > source.Height)
            {
                newWidth = source.Width / (source.Width / x22); 
                newHeight = source.Height / (source.Width / x22);  
            }
            else
            {
                newWidth = source.Width / (source.Height/y22); 
                newHeight = source.Height / (source.Height/y22);  
            }
        }
        else 
        {
            newWidth = source.Width; 
            newHeight = source.Height; 
        }
   
        
        /*
        if (source.Width > source.Height)
        {
            x_calculated = x2;
            y_calculated = (source.Height / (double)source.Width) * y2;
            x_offset = 0;
            y_offset = (x2 - y_calculated) / 2;
        }
        else
        {
            x_calculated = (source.Width / (double)source.Height) * x2;
            y_calculated = y2;
            x_offset = (y2 - x_calculated) / 2;
            y_offset = 0;
        }
         */

       

        Bitmap b = new Bitmap(Convert.ToInt32(newWidth), Convert.ToInt32(newHeight));
        Graphics g = Graphics.FromImage(b);
        g.InterpolationMode = InterpolationMode.HighQualityBicubic;
        //g.FillRectangle(new SolidBrush(Color.FromArgb(127 ,28, 82, 14)), 0, 0, Convert.ToInt32(x2), Convert.ToInt32(y2));
        
        //g.FillRectangle(new SolidBrush(Color.FromArgb(0x1C, 0x52, 0x0E)), 0, 0, Convert.ToInt32(x2), Convert.ToInt32(y2));
        //g.FillRectangle(Brushes.Black, 0, 0, Convert.ToInt32(x2), Convert.ToInt32(y2));
        
       // System.Drawing.Rectangle imageRectangle = new Rectangle(0, 0, Convert.ToInt32(newWidth), Convert.ToInt32(newHeight));
        g.DrawImage(source, 0, 0, Convert.ToInt32(newWidth), Convert.ToInt32(newHeight));
 
        //g.DrawImage(source, (float)x_offset, (float)y_offset, (float)x_calculated, (float)y_calculated);
        g.Dispose();
        return (System.Drawing.Image)b;
    }


    public static void StorePicture(Stream stream, string productID)
    {
        System.Drawing.Image orginal = System.Drawing.Image.FromStream(stream);
        int regularSizeX = RegularSizeX;
        int regularSizeY = RegularSizeY;
        int thumbSizeX = ThumbSizeX;
        int thumbSizeY = ThumbSizeY;

        string imagePath = "~\\";
        imagePath = HttpContext.Current.Server.MapPath(imagePath);

        // delete if exist and save new one
        System.Drawing.Image normal = Resize(orginal, 0, 0, regularSizeX, regularSizeY);
        FileInfo oldNormal = new FileInfo(imagePath + "Images\\Regular\\" + productID + ".jpg");
        if (oldNormal.Exists)
            oldNormal.Delete();
        normal.Save(imagePath + "Images\\Regular\\" + productID + ".jpg");

        // delete if exist and save new one
        FileInfo oldResized = new FileInfo(imagePath + "Images\\Thumbs\\" + productID + ".jpg");
        if (oldResized.Exists)
            oldResized.Delete();
        System.Drawing.Image resized = Resize(orginal, 0, 0, thumbSizeX, thumbSizeY);
        resized.Save(imagePath + "Images\\Thumbs\\" + productID + ".jpg");
    }
}
